package org.tvolkov.mycompany

class ProjectService {
	
	static final def NAME = "name";
	static final def CUSTOMER = "customer";
	static final def DEADLINE = "deadline";
	static final def PARENT = "parentDivision";
	
    def getProjectList(params){
		def name = Division.get(params["id"]);
		return Project.findAllByDivision(name).collect{[
			text: it.name,
			id: ("project" + it.id),
		]};
	}
	
	def getAllProjects(){
		return Project.list().collect{[
			text: it.name,
			id: ("project" + it.id)
		]};
	}
	
	def addNewProject(params){
		println(params);
		def parentDivision = params["parentDivision"];
		if (parentDivision){
			println("addProject: parentDivision: " + parentDivision);
			def proj = new Project();
			if (params[NAME]){
				proj.name = params[NAME];
			}
			if (params[CUSTOMER]){
				proj.customer = params[CUSTOMER];
			}
			if (params[DEADLINE]){
				proj.deadline = Date.parse("MM/dd/yyyy", params[DEADLINE]);
			}
			if (params[PARENT]){
				def division = Division.get(params[PARENT]);
				if (division != null){
					proj.division = division;
				}
			}
			if (proj.save() != null){
				println("addProject: success");
				return ["success": "true"];
			} else {
				println("addProject: fail1");
				return ["success": "false"];
			}
		} else {
			println("addProject: fail2");
			return ["success": "false"];
		}
	}
	
	def removeProject(params){
		Project.get(params["nodeid"]).delete();
//		employeeService.removeEmployees(Employee.findAllByProject(project));
//		project.delete();
		
		return ["success": "true"];
	}
	
	def getProjectDetails(params){
		def project = Project.get(params["nodeid"]);
		println("ProjectService: getProjectDetails: deadline: " + project.deadline.toString());
		return [text: project.name,
				id: ("project" + project.id),
				customer: project.customer,
				deadline: project.deadline.format("MM/dd/yyyy")];	
	}
	
	def editProject(params){
		println("ProjectService: edit: params: " + params);
		def project = Project.get(params["nodeid"]);
		
		if (params["name"]){
			if (project.name != params["name"]){
				project.name = params["name"];
				println("ProjectService: editProject: set name " + params["name"]);
			} else {
				println("ProjectService: editProject: set name fail");
			}
		}
		if (params["customer"]){
			if (project.customer != params["customer"]){
				project.customer = params["customer"];
				println("ProjectService: editProject: set customer " + params["customer"]);
			} else {
				println("ProjectService: editProject: set customer fail");
			}
		}
		if (params["deadline"]){
			if (project.deadline != params["deadline"]){
				project.deadline = Date.parse("MM/dd/yyyy", params["deadline"]);
				println("ProjectService: editProject: set deadline " + params["deadline"]);
			} else {
				println("ProjectService: editProject: set deadline fail");
			}
		}
		
		if (project.save()){
			return ["success": "true"];
		} else {
			return ["success": "false"];
		}
	}
}
